<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>PriorityQueue</title>
  </head>
  <body>
    <script>
      class QueueElement {
        constructor(element, priority) {
          this.element = element;
          this.priority = priority;
        }
      }
      class PriorityQueue {
        constructor() {
          this.arr = [];
        }
        enqueue(element, priority) {
          let queueElement = new QueueElement(element, priority);
          if (this.arr.length === 0) {
            this.arr.push(queueElement);
          } else {
            let isAdded = false;
            for (let i = 0; i < this.arr.length; i++) {
              if (queueElement.priority < this.arr[i].priority) {
                this.arr.splice(i, 0, queueElement);
                isAdded = true;
                break;
              }
            }
            if (!isAdded) {
              this.arr.push(queueElement);
            }
          }
        }
        // 出栈
        dequeue() {
          return this.arr.shift();
        }
        // 返回队列第一个元素
        front() {
          return this.arr[0];
        }
        isEmpty() {
          return this.arr.length === 0;
        }
        size() {
          return this.arr.length;
        }
        toString() {
          var str = "";
          for (var i = 0; i < this.arr.length; i++) {
            str += this.arr[i].priority + '-' + this.arr[i].element + " ";
          }
          return str;
        }
      }
      let pq = new PriorityQueue();
      pq.enqueue('a',1)
      pq.enqueue('b',12)
      pq.enqueue('c',10)
      console.log(pq.toString())

    </script>
  </body>
</html>
